#随机生成20个[1, 20]的正整数，统计每个数字出现的次数，排序并降序输出次数最多的前三个数字及次数
import random
l = 20
nums = [random.randint(1,20) for i in range(l)]
print(nums)

#1.使用字典统计排序
from collections import defaultdict

d = defaultdict(lambda: 0)
for i in nums:
    d[i] +=1
#print(d.items())
print((sorted(d.items(),key=lambda x:x[1],reverse=True)[:3]))

print('#' * 80)

#2.使用列表统计排序，效率低
count_nums = []
state = [False] * l

for i in range(l):
    count = 1
    if state[i]: continue
    for j in range(i+1,l):
        if state[j]: continue
        if nums[i] == nums[j]:
            count +=1
            state[j] = True
    if count != 1:
        state[i] = True
        count_nums.append((nums[i],count))
    else:
        count_nums.append((nums[i],count))
#print(state)
#print(count_nums)
print((sorted(count_nums,key=lambda x:x[1],reverse=True)[:3]))




# 完成的不错
